using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;

public partial class Admin_ProductCatList : System.Web.UI.Page
{
    #region Region
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    protected string hidColSort
    {
        get
        {
            object o = ViewState["hidColSort"];
            if (o == null) return "[" + ProductCat._Table + "].[" + ProductCatColumns.SortOrder + "]";
            return o.ToString();
        }
        set
        {
            ViewState["hidColSort"] = value;
        }
    }
    public string Title
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["Title"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["Title"];
        }
    }

    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        LoadCurent();
        LoadData();
        txtSearch.Text = this.Title;
    }
    #region LoadCurent
    private void LoadCurent()
    {
        HtmlGenericControl ulProduct = (HtmlGenericControl)Master.FindControl("ulProduct");
        ulProduct.Attributes.Add("class", "current");
        HtmlControl divSelectSubPro = (HtmlControl)Master.FindControl("divSelectSubPro");
        divSelectSubPro.Attributes.Add("class", "select_sub show");
        HtmlGenericControl liCatPro = (HtmlGenericControl)Master.FindControl("liCatPro");
        liCatPro.Attributes.Add("class", "sub_show");

    }
    #endregion
    #region LoadData
    private string username = string.Empty;
    private void LoadData()
    {
        Criteria cri = new Criteria();
        hyperlinkPager.PageSize = 10;
        NoCountStart = (hyperlinkPager.PageSize * (hyperlinkPager.CurrentIndex - 1)) + 1;
        int totalrecord = 0;
        if (!string.IsNullOrEmpty(this.Title))
            cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.Name + "]", this.Title, Condition.LIKE, Condition.AND);

        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.CatLevel + "]", 0, Condition.EQUAL, Condition.AND);
        ProductCatCollection _ProductCatCollection = ProductCatService.GetProductCatListByParam(hidColSort, OrderDirection.ASC, hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out totalrecord);

        rptList.DataSource = _ProductCatCollection;
        rptList.DataBind();
        hyperlinkPager.ItemCount = totalrecord;
        if ((float)totalrecord / (float)hyperlinkPager.PageSize <= 1)
            hyperlinkPager.Visible = false;

    }
    protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ProductCat _ProductCat = (ProductCat)e.Item.DataItem;
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            HtmlTableRow tr = (HtmlTableRow)e.Item.FindControl("tr");
            Literal ltrTitleChild = (Literal)e.Item.FindControl("ltrTitleChild");
            TextBox txtOrder = (TextBox)e.Item.FindControl("txtOrder");
            CheckBox ckEnable = (CheckBox)e.Item.FindControl("ckEnable");
            
            ltrTitleChild.Text = _ProductCat.Name;
            txtOrder.Text = _ProductCat.SortOrder.ToString();
            ckEnable.Checked = _ProductCat.Enable;
            hidID.Value = _ProductCat.ProCateID.ToString();
            int STT = e.Item.ItemIndex +1;
            Literal ltrNo = (Literal)e.Item.FindControl("ltrNo");
            ltrNo.Text = STT.ToString();
            if (STT % 2 != 0)
            {
                tr.Attributes["class"] = "alternate-row";
            }
            else
            {
                tr.Attributes["class"] = "none";
            }
        }
    }   

    protected void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        hyperlinkPager.CurrentIndex = currnetPageIndx;
        LoadData();
    }
    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string commandName = e.CommandName;
        object obj = new object();
        switch (commandName)
        {
            case "sortName":
                obj = "[" + ProductCat._Table + "].[" + ProductCatColumns.Name + "]";
                break;
            case "SortOrder":
                obj = "[" + ProductCat._Table + "].[" + ProductCatColumns.SortOrder + "]";
                break;
           
            default:
                break;
        }
        if (OrderDirection.ASC == OrderDirection.ASC)
            Session["Order"] = OrderDirection.DESC;
        else
            Session["Order"] = OrderDirection.ASC;
        this.hidColSort = obj.ToString();
        LoadData();
    }
    #endregion
    #region Delete Account

    
    protected void lndelete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            HtmlInputCheckBox chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
            if (chkDelete.Checked == true)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                string strhidID = hidID.Value;
                long ID = long.Parse(strhidID);

                try
                {
                    #region Check Order data
                    Criteria cri = new Criteria();
                    cri.add(ProductCatColumns.Parrentid, ID, Condition.EQUAL, Condition.AND);
                    ProductCatCollection _ProductCatCollection = ProductCatService.GetProductCatListByParam(ProductCatColumns.ProCateID, OrderDirection.ASC, cri.Criter);
                    if (_ProductCatCollection.Count > 0)
                    {
                        lb_error.Text = "Dont's delete this item because it is getting using another items";
                    }
                    else
                    {
                        ProductCatService.DeleteProductCat(ID);
                    }
                    #endregion
                }
                catch
                {
                    lb_error.Text = "Dont's delete this item because it is getting using another items";
                }
            }
        }
        LoadData();
    }
    #endregion
    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            HiddenField hidID = (HiddenField)item.FindControl("hidID");
            CheckBox ckEnable = (CheckBox)item.FindControl("ckEnable");
            ProductCat _ProductCat = ProductCatService.GetProductCat(long.Parse(hidID.Value));
            TextBox txtOrder = (TextBox)item.FindControl("txtOrder");

            if (_ProductCat != null)
            {
                _ProductCat.SortOrder = long.Parse(txtOrder.Text.Trim());
                _ProductCat.Enable = ckEnable.Checked;
                ProductCatService.UpdateProductCat(_ProductCat);
            }
        }
        LoadData();
    }
    protected void btnSearch_OnClick(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("ProductCatList.aspx?Title={0}", txtSearch.Text.Trim()));
    }
    protected void btnSearchAll_OnClick(object sender, EventArgs e)
    {
        Response.Redirect("ProductCatList.aspx");
    }
}
